﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ZLDC.Common.Estate;
using ZLDC.DAL.Estate;

namespace ZLDC.Topo.BLL.Estate
{
    public class DeptTree
    {
        private string sTopDeptId = string.Empty; private string sLimitDeptId = string.Empty; protected System.Text.StringBuilder sbDeptHtml = new System.Text.StringBuilder();
        private string sKeyName = "estate-depttree-";

        public DeptTree()
        {
        }

        public DeptTree(string _sTopId, string _limitDept)
        {
            sTopDeptId = _sTopId;
            sLimitDeptId = _limitDept;
            sKeyName += _sTopId + "_" + _limitDept;
        }

        public void ClearCache()
        {
            System.Collections.IDictionaryEnumerator iCache = System.Web.HttpRuntime.Cache.GetEnumerator();

            while (iCache.MoveNext())
            {
                if (iCache.Key.ToString().IndexOf(sKeyName) > -1 && System.Web.HttpRuntime.Cache[iCache.Key.ToString()] != null)
                {
                    System.Web.HttpRuntime.Cache.Remove(iCache.Key.ToString());
                }
            }
        }

        public string GetSonDept(string sId)
        {
            sKeyName += "son_" + sId;
            string sSonDeptHtml = string.Empty;

            if (System.Web.HttpContext.Current.Cache[sKeyName] != null)
            {
                sSonDeptHtml = (string)System.Web.HttpContext.Current.Cache[sKeyName];
            }
            else
            {
                System.Text.StringBuilder sbDeptHtml = new System.Text.StringBuilder();
                DataSet dsDep = DeptDAL.GetDeptData();
                System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
                dvTree.RowFilter = "[parent_id] = " + sId;
                if (dvTree.Count > 0)
                {
                    foreach (System.Data.DataRowView datav in dvTree)
                    {
                        if (GetExistsDeptSon(datav["id"].ToString(), dsDep))
                        {
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "_1' ><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>");
                            sbDeptHtml.Append("<ul class='ajax'>");
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'>{url:../Ajax/GetDeptTree.aspx?id=" + datav["id"].ToString() + "}</li>");
                            sbDeptHtml.Append("</ul>");
                            sbDeptHtml.Append("</li>");
                        }
                        else
                        {
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'><span class='text'><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span></li>");
                        }
                    }
                }
                sSonDeptHtml = sbDeptHtml.ToString();
                System.Web.HttpContext.Current.Cache.Insert(sKeyName, sSonDeptHtml, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
            }

            return sSonDeptHtml;
        }

        public string GetZlhqDept()
        {
            string sSql = "select id,name,grade,parent_id from t_zlhq_t_s_alldept where dltflg = 0 ";
            if (sLimitDeptId != string.Empty)
            {
                sSql += " and id in (" + sLimitDeptId + ") ";
            }
            if (sTopDeptId == string.Empty)
            {
                sSql = "select top 1 * from (" + sSql + ") tt where grade is not null order by grade";
                using (_Database db = new _Database("zlhq_user"))
                {
                    DataSet ds = db.Query(sSql);

                    if (ds.Tables[0].Rows.Count > 0)
                        sTopDeptId = ds.Tables[0].Rows[0]["id"].ToString();
                    else
                        sTopDeptId = "-1";
                }
            }

            AddTree(sTopDeptId, DeptDAL.GetDeptData());

            return sbDeptHtml.ToString();
        }

        protected void AddTree(string ID, DataSet dsDep)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            dvTree.RowFilter = "[id] = " + ID;
            sbDeptHtml.Append("<ul class='simpleTree'>\n");
            foreach (System.Data.DataRowView datav in dvTree)
            {
                sbDeptHtml.Append("<li class='root' id='" + datav["id"].ToString() + "'><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>\n");
                AddTree2(datav["id"].ToString(), dsDep, string.Empty);

                sbDeptHtml.Append("</li>\n");
            }
            sbDeptHtml.Append("</ul>\n");
        }

        protected void AddTree2(string ParentID, System.Data.DataSet dsDep, string sTmpTemplate)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            dvTree.RowFilter = "[parent_id] = " + ParentID;
            if (dvTree.Count > 0)
            {
                sbDeptHtml.Append("<ul>\n");
                foreach (System.Data.DataRowView datav in dvTree)
                {
                    sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>\n");
                    AddTree3(datav["id"].ToString(), dsDep, string.Empty);
                    sbDeptHtml.Append("</li>\n");
                }
                sbDeptHtml.Append("</ul>\n");
            }
        }

        protected void AddTree3(string ParentID, System.Data.DataSet dsDep, string sTmpTemplate)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            dvTree.RowFilter = "[parent_id] = " + ParentID;
            if (dvTree.Count > 0)
            {
                sbDeptHtml.Append("<ul class='ajax'>\n");
                sbDeptHtml.Append("<li id='" + ParentID + "'>{url:../Ajax/GetDeptTree.aspx?id=" + ParentID + "}</li>\n");
                sbDeptHtml.Append("</ul>\n");
            }
        }

        private bool GetExistsDeptSon(string dpid, DataSet ds)
        {
            System.Data.DataView dvTree = new System.Data.DataView(ds.Tables[0]);
            dvTree.RowFilter = "[parent_id] = " + dpid;
            if (dvTree.Count > 0)
                return true;
            else
                return false;
        }

    }
}
